#ifndef LEETCODE_CN_CPP_PROBLEMS_3_H
#define LEETCODE_CN_CPP_PROBLEMS_3_H

#include <algorithm>
#include <array>
#include <string>

using namespace std;

namespace problem_3
{

class Solution
{
private:
    array<bool, 256> seen;

public:
    int lengthOfLongestSubstring(string s)
    {
        fill(seen.begin(), seen.end(), false);
        int maxLen = 0, i = 0, j = 0;
        while (j < s.length())
        {
            auto c = s[j];
            j += 1;
            if (!seen[c])
            {
                maxLen = max(j - i, maxLen);
                seen[c] = true;
            }
            else
            {
                while (i < j && s[i] != c)
                {
                    seen[s[i]] = false;
                    i += 1;
                }
                i += 1;
            }
        }
        return maxLen;
    }
};

} // namespace problem_3

#endif //LEETCODE_CN_CPP_PROBLEMS_3_H